package org.electric.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.electric.bean.CompanyBean;
import org.electric.service.Company_service;
import org.electric.util.WriterUtil;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet("/CompanyServlet")
public class Company_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CompanyBean company=new CompanyBean();
	Company_service cs = new Company_service();
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Company_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = req.getParameter("_method");
		if("selectAllCompany".equals(method)){
			selectAllCompany(req,res);
		}else if("selectAll".equals(method)){	
			selectAll(req,res);
		}else if("updateCompany".equals(method)){
			try {
				updateCompany(req,res);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("deletCompany".equals(method)){
			deletCompany(req,res);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	//查询所有
	public void selectAllCompany(HttpServletRequest req,HttpServletResponse res){
		String companys = req.getParameter("company_user");
		String passwords = req.getParameter("company_password");
		company.setCompany_user(companys);
		company.setCompany_password(passwords);
		
	}
	//查询当前所有
	public void selectAll(HttpServletRequest req,HttpServletResponse res){
		
	}
	
	//修改-----------------------------------
	public void updateCompany(HttpServletRequest req,HttpServletResponse res) throws Exception{
		String company_password=req.getParameter("company_password").trim();
		String company_name=req.getParameter("company_name").trim();
		String register_money=req.getParameter("register_money").trim();
		String employee_num=req.getParameter("employee_num").trim();
		String address=req.getParameter("address").trim();
		String tel=req.getParameter("tel").trim();
		company.setCompany_password(company_password);
		company.setCompany_name(company_name);
		company.setRegister_money(Integer.parseInt(register_money));
		company.setEmployee_num(Integer.parseInt(employee_num));
		company.setAddress(address);
		company.setTel(tel);
		ObjectMapper om = WriterUtil.writeTopage();
		Map<String,String> map = new HashMap<String,String>();
		if(cs.updateCompanyById(company)){
			map.put("flag", "1");
		}else{
			map.put("flag", "0");
		}
		map.put("flag", "-1");
	
	}
	//删除
	public void deletCompany(HttpServletRequest req,HttpServletResponse res){
		
	}


}
